package com.dao;

import com.model.Goods;
import com.utils.DbUntil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class GoodsDao {

    private Connection connection;

    //在这里初始化  这里获取链接
    public GoodsDao() {
        connection = DbUntil.getConnection();
    }

    //公有方法 获取所有商品信息
    public List<Goods> getAllGoods() {
        List<Goods> goods = new ArrayList<Goods>();
        try {
            Statement statement = connection.createStatement();
            //这里是查询select * from goods
            ResultSet rs = statement.executeQuery("select * from goods");
            while (rs.next()) {
                Goods good = new Goods();
                good.setGoods_id(rs.getString("goods_id"));
                good.setGoods_name(rs.getString("goods_name"));
                good.setGoods_price(rs.getString("goods_price"));
                good.setCategory_id(rs.getString("category_id"));
                good.setGoods_describe(rs.getString("goods_describe"));
                good.setStatus(rs.getString("status"));
                good.setGoods_times("goods_times");
                goods.add(good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return goods;
    }

    //通过类别查找商品
    public List<Goods> getGoodsByCategoryId(String categoryId){
        List<Goods> goods = new ArrayList<Goods>();
        try {
            Statement statement = connection.createStatement();
            String sql = "select * from goods where category_id='"+categoryId+"' AND status=1";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Goods good = new Goods();
                good.setGoods_id(rs.getString("goods_id"));
                good.setGoods_name(rs.getString("goods_name"));
                good.setGoods_price(rs.getString("goods_price"));
                good.setCategory_id(rs.getString("category_id"));
                good.setGoods_describe(rs.getString("goods_describe"));
                good.setStatus(rs.getString("status"));
                good.setGoods_times("goods_times");
                goods.add(good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goods;
    }

    //通过id查找商品
    public Goods getGoodsByGoodsId(String goodsId){
        List<Goods> goods = new ArrayList<Goods>();
        try {
            Statement statement = connection.createStatement();
            String sql = "select * from goods where goods_id='"+goodsId+"'AND status=1";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Goods good = new Goods();
                good.setGoods_id(rs.getString("goods_id"));
                good.setGoods_name(rs.getString("goods_name"));
                good.setGoods_price(rs.getString("goods_price"));
                good.setPhoto_address(rs.getString("photo_address"));
                good.setCategory_id(rs.getString("category_id"));
                good.setGoods_describe(rs.getString("goods_describe"));
                good.setStatus(rs.getString("status"));
                good.setGoods_times("goods_times");
                goods.add(good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(goods.size()>0){ return goods.get(0); }
        else {return null;}
    }

    //获取前6名的商品
    public List<Goods> getGoods_topSix(){
        List<Goods> goods = new ArrayList<Goods>();
        try {
            Statement statement = connection.createStatement();
            String sql = "SELECT * FROM goods order by goods_times desc limit 6";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Goods good = new Goods();
                good.setGoods_id(rs.getString("goods_id"));
                good.setGoods_name(rs.getString("goods_name"));
                good.setGoods_price(rs.getString("goods_price"));
                good.setPhoto_address(rs.getString("photo_address"));
                good.setCategory_id(rs.getString("category_id"));
                good.setGoods_describe(rs.getString("goods_describe"));
                good.setStatus(rs.getString("status"));
                good.setGoods_times("goods_times");
                goods.add(good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(goods.size()>0){
            return goods;
        }
        else {
            return null;
        }
    }

}
